
	
************************************************************************************************************************************************
************************************************************************************************************************************************
*NPV Distributions
************************************************************************************************************************************************
************************************************************************************************************************************************

cd $directory


************************************************
*Set list of margins; becomes points on x axis
************************************************

global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"


global LIST = "69001 69101 69006 69106 69201" //  Economist Silver correctedEconomist correctedSilver GSE2010


global SD = 100000
global TURNOUT = 150000000

************************************************
*Storage for Summary Statistics
************************************************

global STEP = .002	//step along horizontal axis for density plot
global STEP = .004	//step along horizontal axis for density plot
global BINSTART= .42 					//rightmost point of lowest bin
global BINEND = 1 - $BINSTART + $STEP 	//rightmost point of last bin

matrix define REC		=J(30, 10, .) 		//table of summary of reversal rates; columns are models, rows are critical margins of votes dropped from winner or gained by loser
matrix define RECDECWIN	=J(30, 10, .) 		//table of summary of reversal rates; condl on D EC WINS
matrix define RECRECWIN	=J(30, 10, .) 		//table of summary of reversal rates; condl on D EC WINS
matrix define RPV		=J(30, 10, .) 		//table of summary of NPV reversal rates;
matrix define HIST		=J(200, 200, .) 	//table for figure: hist

//define bin starts as first row of F
local row=4
forvalues i=$BINSTART($STEP)$BINEND{ //to hold histogram heights
	matrix HIST[`row', 1] = `i'
	local row = `row' + 1
}

************************************************
*Outer loop - open datasets
************************************************

global LOOP = 2

foreach file in $LIST{

	use "source_data`c(dirsep)'`file'.dta", clear

************************************************
*stats and vars
************************************************

//count states 
local error=0
local counter=1
while `error' != 111 {
	qui cap d total_ev_`counter'
	local error=_rc
	local counter=`counter'+1
}
global STATECOUNT = `counter'-2 
di "$STATECOUNT"
local state_count = `counter'-2 


	//make vars that summarize each election
	qui egen pvotes = rowtotal(twoPartyVotes_*)
	forvalues i = 1(1)`state_count'{
		qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
	}
	qui egen rvotes = rowtotal(rvotes_*)
	qui gen pvshare = rvotes/pvotes
	qui gen margin_share = abs(0.5-pvshare)*2
	qui gen margin_votes = margin_share*pvotes

	forvalues i = 1(1)`state_count'{
		qui gen rwin_`i' = R_pvShare_`i' > .5
		qui gen rev_`i' = rwin_`i' * total_ev_`i'
	}
	qui egen revn_total= rowtotal(rev_*)
	qui egen ev_denominator = rowtotal(total_ev_*)
	qui gen rwin = revn_total/ev_denominator > 0.5
	qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
	qui gen cf_revn_total  = revn_total
	qui gen cf_rwin = rwin

	
	//This loop identifies which states can flip election on their own (swing==1) and if so, 
	//how many votes must be dropped to do it(vmargin==X)
	forvalues i = 1(1)`state_count'{
		qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2 //multiply by 2 to get the number of votes that separate candidates ((1-Rshare)-Rshare) = 1-2rshare
		qui replace cf_revn_total = revn_total //reset each loop
		qui replace cf_rwin = rwin //reset each loop
		qui replace cf_revn_total = cf_revn_total - total_ev_`i' if rwin_`i' == 1 //substract state to ECV count if R won the sim
		qui replace cf_revn_total = cf_revn_total + total_ev_`i' if rwin_`i' == 0 //add state from ECV count if R lost the sim
		qui replace cf_rwin =  cf_revn_total/ev_denominator > 0.5 //did R win in the counterfactual flip of the state?
		qui gen swing_`i' = cf_rwin != rwin //did the outcome of the election change? 
		//^this allows a change from not a tie to a tie or vice versa to count as a change
		qui replace vmargin_`i' = . if swing_`i' == 0 //keep vmargin blank if the state can't flip the election alone
		label var vmargin_`i' "The margin in vote counts in state `i'"
		foreach marg in $MARGLIST{
			qui gen swing`marg'_`i' = swing_`i'==1 & vmargin_`i'<`marg'
			label var swing`marg'_`i' "Binary: is vmargin_`i' less than `marg'"
		}
	}
	
	gen closest_flippable_state=.
	qui egen min_vmargin = rowmin(vmargin_*) 
	label var min_vmargin "minimum votes needed to flip closest state"
	qui gen tipping_state = .
	forvalues i = 1(1)`state_count'{
		qui replace closest_flippable_state = `i' if min_vmargin == vmargin_`i'
	}

	//EC Reversals
	egen swingers = rowtotal(swing_*)
	foreach marg in $MARGLIST{
		qui egen swing`marg' = rowtotal(swing`marg'_*)
		qui replace swing`marg' = 0 if swing`marg' == .
		qui gen notReversed`marg' = swing`marg'==0
		label var notReversed`marg' "EC not reversed by one state dropping `marg' votes"
		qui gen Reversed`marg' = (swing`marg'>0 & swing`marg'<51)
		qui replace Reversed`marg' = 0 if swing`marg' == .
		label var Reversed`marg' "EC reversed by at least one state dropping `marg' votes"
		
	}
	drop swing`marg'_*
	
	//NPV Reversals
	foreach marg in $MARGLIST{
		qui gen PVswing`marg'=0
		qui replace PVswing`marg' = 1 if margin_votes<`marg'
		
	}	

	
	qui replace swingers = 0 if swingers == .
	qui gen ec_margin = min_vmargin
	qui gen pv_margin = margin_votes
	
	qui gen zero = swingers == 0
	label var swingers "number of states that could swing election"
	
	*hist swingers, ///
	*	name(swingers, replace) 

		
	//more state level variables
	forvalues s=1(1)$STATECOUNT{
		//rename whig variables, as needed
		cap rename  W_pvShare_`s'  R_pvShare_`s'
		//state vote counts for Rs
		qui gen r_votes_`s' = R_pvShare_`s'*twoPartyVotes_`s'
		//state evs for Rs
		qui gen r_ev_`s' = total_ev_`s' if R_pvShare_`s'>=0.50
		qui replace r_ev_`s' = 0 if R_pvShare_`s'<0.50
	
	}
	
	//buildup string for row totals
	local votestring = "r_votes_1"
	local evstring = "r_ev_1"
	local totalvotesstring = "twoPartyVotes_1"
	local totalevstring = "total_ev_1"
	forvalues s=2(1)$STATECOUNT{ 
		local votestring = "`votestring' r_votes_`s'" 	// R votes row total string
		local evstring = "`evstring' r_ev_`s'"			// R ev row total string
		local totalvotesstring = "`totalvotesstring' twoPartyVotes_`s'" //all votes
		local totalevstring = "`totalevstring' total_ev_`s'" //all evs
	}
	
	//sum to national outcomes
	qui egen r_votes_natl = rowtotal(`votestring')
	qui egen r_ev_natl = rowtotal(`evstring')
	qui egen totalvotes_natl = rowtotal(`totalvotesstring')
	qui egen totalev_natl = rowtotal(`totalevstring')
	
	qui gen cutoff = floor(totalev_natl/2) + 1 
	qui gen r_share_natl = r_votes_natl/totalvotes_natl
	qui gen r_ev_win = (r_ev_natl >= cutoff)
	qui gen r_pv_win = (r_votes_natl>=(totalvotes_natl/2))
	qui gen inv = r_ev_win!=r_pv_win 
	label var r_share_natl "R share nationally"
	label var r_ev_win "R ev win nationally"
	label var r_pv_win "R pv win nationally"
	label var inv "inversion"

	************************************************
	*Data for  figures
	************************************************
	local col = $LOOP
	di `col'
	di `file'
	matrix REC[1, `col'] 		= `file'
	matrix RECDECWIN[1, `col'] 	= `file'
	matrix RECRECWIN[1, `col'] 	= `file'
	matrix RPV[1, `col'] 		= `file'

	local row = 2 //place marglist in rows starting at 2
	//stores fraction of sims reversed by indicated margin
	foreach marg in $MARGLIST{

		matrix REC[`row',1] = `marg'
		matrix RECDECWIN[`row',1] = `marg'
		matrix RECRECWIN[`row',1] = `marg'
		matrix RPV[`row',1] = `marg'
		
		qui summ Reversed`marg'
		matrix REC[`row',`col'] = r(mean)
		qui summ Reversed`marg' if dwin==1
		matrix RECDECWIN[`row',`col'] = r(mean)
		qui summ Reversed`marg' if rwin==1
		matrix RECRECWIN[`row',`col'] = r(mean)
		
		qui summ PVswing`marg'
		matrix RPV[`row',`col'] = r(mean)		
		
		local row = `row'+1
	}
	
	
	matrix list REC
	
	************************************************
	*Data for density figures
	************************************************
	local col = $LOOP
	
	matrix HIST[1,`col'] 	= `file'

	local row = 4
	forvalues i= $BINSTART($STEP)$BINEND {
		local last = `i'-$STEP
		local next = `i'
		matrix HIST[`row', 1] = `i'- $STEP/2	// first column for horizontal axis
		qui count if r_share_natl>`last' & r_share_natl<=`next' 
		local den = r(N)
		qui count if r_share_natl>`last' & r_share_natl<=`next' & r_ev_win==1 
		local num = r(N)	
		local win = `num'/`den'
		matrix HIST[`row',`col'] = `den'
		local row = `row' + 1
	}

************************************************
*Outer loop - close datasets/convert matrices
************************************************
	
global LOOP = $LOOP +1		
di "$LOOP"
	
}
global ENDLOOP = $LOOP

cap drop REC*
svmat REC
cap drop RPV*
svmat RPV
//cap drop NPV*
//svmat NPV
cap drop HIST*
svmat HIST

//align the bar heights of histograms, given the 10k v 40k sim runs
replace HIST6=HIST6*4

************************************************
*Plot
************************************************




local labsize = "small"
twoway ///
	(connected HIST2 HIST1 in 2/100 ,  lw(thick)  lpattern(..-) lcolor(black) ms(none) ) ///
	(connected HIST3 HIST1 in 2/100 , lw(thin) lpattern(--.) lcolor(black) ms(none) ) ///
	(connected HIST4 HIST1 in 2/100 , lw(medium)  lpattern(longdash) lcolor(black) ms(none) ) ///
	(connected HIST5 HIST1 in 2/100 , lw(medium) lpattern(shortdash) lcolor(black) ms(none) ) ///
	(connected HIST6 HIST1 in 2/100 , lw(medium) lpattern(dash) lcolor(black) ms(none) ) ///
	, ///
	aspect(.5) ///
	ylabel(0 " ", nogrid labsize(medium) axis(1)) ///
	xlabel(.42(.02).58, nogrid labsize(`labsize')) ///
	xline(.5, lcolor(gs11) lpattern(dash) ) ///
	xtitle("Republican Share of NPV", size(`labsize')) ///
	ytitle("Probability", size(`labsize') axis(1)) ///
	graphr(c(white) lc(white)) ///
	legend(order(1 "Gelman et al., 2020 (uncorrected)" 3 "Gelman et al., 2020 (corrected)"  2 "Silver, 2020 (uncorrected)" 4 "Silver, 2020 (corrected)" 5 "Gelman, Silver, Edlin, 2012") position(2) col(1) ring(0) region(lwidth(thin)) size(small))  ///
	name(histoverlay_1, replace) 
	
	graph export "figures`c(dirsep)'FigureA4a.pdf", replace
	

	
	
************************************************************************************************************************************************
************************************************************************************************************************************************
*ECV Distributions
************************************************************************************************************************************************
************************************************************************************************************************************************

cd $directory


************************************************
*Set list of margins; becomes points on x axis
************************************************

global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"

global LIST = "69001 69101 69006 69106 69201" //  Economist Silver correctedEconomist correctedSilver GSE2010


************************************************
*Outer loop - open datasets
************************************************

global LOOP = 2

foreach file in $LIST{

	use "source_data`c(dirsep)'`file'.dta", clear

	
************************************************
*Make vars
************************************************	

	if `file'==52201{
		local simlabel = "Geruso et al., 2022"
		local ycoord = .0185
	}
	if `file'==69001{
		local simlabel = "Gelman et al., 2020 uncorrected"
		local ycoord = .015
	}
	if `file'==69101{
		local simlabel = "Silver, 2020 uncorrected"
		local ycoord = .015
	}
	if `file'==69006{
		local simlabel = "Gelman et al., 2020 corrected"
		local ycoord = .015
	}
	if `file'==69106{
		local simlabel = "Silver, 2020 corrected"
		local ycoord = .015
	}
	if `file'==69201{
		local simlabel = "Gelman, Silver, Edlin, 2012"
		local ycoord = .05
	}

	
	//count states 
	local error=0
	local counter=1
	while `error' != 111 {
		qui cap d total_ev_`counter'
		local error=_rc
		local counter=`counter'+1
	}
	global STATECOUNT = `counter'-2 
	di "$STATECOUNT"
	local state_count = `counter'-2 

	//make vars that summarize each election
	qui egen pvotes = rowtotal(twoPartyVotes_*)
	forvalues i = 1(1)`state_count'{
		qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
	}
	qui egen rvotes = rowtotal(rvotes_*)
	qui gen pvshare = rvotes/pvotes
	qui gen margin_share = abs(0.5-pvshare)*2
	qui gen margin_votes = margin_share*pvotes

	forvalues i = 1(1)`state_count'{
		qui gen rwin_`i' = R_pvShare_`i' > .5
		qui gen rev_`i' = rwin_`i' * total_ev_`i'
	}
	qui egen revn_total= rowtotal(rev_*)
	qui egen ev_denominator = rowtotal(total_ev_*)
	qui gen rwin = revn_total/ev_denominator > 0.5
	qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
	qui gen cf_revn_total  = revn_total
	qui gen cf_rwin = rwin

	qui gen inv=0
	qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)

	
	local labsize = "large"
	local textsize = "medium"
	hist revn_total  , ///
		xtitle("Republican EC Votes", size(`labsize')) xlabel(0 100  270 400 538, nogrid labsize(`labsize')) ///
		bfcolor(gs8) blcolor(gs8) ///	
		graphregion(color(white)) plotregion(color(white)) ///
		xsc( r(0 538)) ///
		start(0) fysize(25) width(5) barwidth(3.5) ///
		ylabel(none) xline(270, lcolor(gs11) lpattern(dash) ) ///		
		ytitle("") ///
		text(`ycoord' 300 "`simlabel'" , color(gs8) place(e) justification(left) size(`textsize')) ///
		name(hist_`file', replace) saving(figures`c(dirsep)'hist_`file', replace)		
	
************************************************
*Outer loop
************************************************
	
global LOOP = $LOOP +1		
di "$LOOP"
	
}
global ENDLOOP = $LOOP

************************************************
*Figures
************************************************

graph combine "figures`c(dirsep)'hist_69001.gph" "figures`c(dirsep)'hist_69006.gph" "figures`c(dirsep)'hist_69101.gph" "figures`c(dirsep)'hist_69106.gph" "figures`c(dirsep)'hist_69201.gph", ///
		graphregion(color(white)) rows(3) cols(1) xsize(10) ysize(5) xcommon
		
graph export "figures`c(dirsep)'FigureA4b.pdf", replace

